package cn.serviceImpl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.service.ICustomerService;


@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService {

	@Autowired
	private CustomerRepository customerRepository;
	
	@Override
	public List<Customer> findNoAssociationCustomers() {
		
		return customerRepository.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findAllAssociationCustomers(String fixedAreaId) {
		
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public void associationCustomersToFixedArea(String customerIdStr,
			String fixedAreaId) {
		//切割字符串
		
		customerRepository.clearFixedAreaId(fixedAreaId);
		
		if(StringUtils.isBlank(customerIdStr)){
			return;
		}
		String[] array=customerIdStr.split(",");
		for (String string : array) {
			Integer id=Integer.parseInt(string);
			customerRepository.updateFixedAreaId(fixedAreaId,id);
		}
	}
	
	//保存客户
	@Override
	public void regist(Customer customer) {
		customerRepository.save(customer);
	}

}
